package com.zhongwang.mms.module.jiaGongMaterial.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.basic.service.TDeptService;
import com.zhongwang.mms.module.jiaGongMaterial.dao.MaterialInDao;
import com.zhongwang.mms.module.jiaGongMaterial.model.JMaterialAllModel;
import com.zhongwang.mms.module.jiaGongMaterial.service.MaterialStockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 原料出入库查询 控制层
 * @Author: China.wsg
 * @Date: 2020/04/01 11:23
 */
@Controller
@RequestMapping("/materialAccountQuery")
public class MaterialAccountController extends BaseController {
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;
    @Autowired
    private MaterialStockService materialStockService;
    @Autowired
    private MaterialInDao materialInDao;
    @Autowired
    private TDeptService deptService;
    /**
     * 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    public String project(Model model) {

        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原材料甲供件库"));
        return "jiaGongMaterial/materialAccount/page";
    }
    /**
     * 列表
     *
     * @param jMaterialAllModel 查询条件
     * @return list
     */
    @ResponseBody
    @PostMapping("/list")
    public JsonResult list(JMaterialAllModel jMaterialAllModel, @RequestParam(value = "whiIdLists[]", required = false) List<String> whiIdList) {
        startPage();
        List<JMaterialAllModel> list = materialStockService.findOutcome(jMaterialAllModel,whiIdList);
        return jsonResult(list);
    }
    /**
     * 列表 导出
     * @param jMaterialAllModel 查询条件
     * @return list
     */
    @PostMapping("/exportExcel")
    @ResponseBody
    public JsonResult exportExcel(JMaterialAllModel jMaterialAllModel) {
        return materialStockService.exportExcel(jMaterialAllModel);
    }
    /**
     * 入库/出库 查看页面
     *
     * @param odrId 主单Id
     * @param model
     * @return 页面路径
     */
    @GetMapping("/view")
    public String view(@RequestParam(value = "odrId", required = false) Integer odrId, @RequestParam(value = "odrType", required = false) Integer odrType, Model model) {

        String prefix = "";
        if (odrType == 1 || odrType == 2 || odrType == 6) {
            // 入库单
            prefix = "jiaGongMaterial/materialAccount/info/viewIn";
        } else if (odrType == 4 || odrType == 5) {
            // 出库单
            prefix = "jiaGongMaterial/materialAccount/info/viewOut";
        }
        model.addAttribute("dept", deptService.findDeptInfo(new TDept()));
        model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原材料甲供件库"));
        model.addAttribute("order", materialInDao.selectMaterialById(odrId));
        return prefix;
    }
}
